/**
 * 文件名称：hyui.core.js
 * 功能描述：HY-UI核心JS文件,所以HY-UI的插件均依赖此文件.
 *
 * 该文件包含基本方法和通用方法,非重大BUG一般情况不更改此文件函数.
 */
(function($, window, document) { // namespace
	window["hy"] = window["hy"] || {}; // jQuery bind
	hy.$ = $;
	hy.jQuery = $; // dom bind
	hy.$window = window;
	hy.$document = document;

	/* commom method */
	/**
	 * HTML标签组装函数
	 *
	 * @param s 含有html标签的字符串
	 * 参数说明
	 * E                           htmlz("div") = "<div></div>"
	 * E#id                        htmlz("div#main") = "<div id="main"></div>"
	 * E.class                     htmlz("div.ui.message") = "<div class="ui message"></div>"
	 * E {prop:value}              htmlz("div {width:200px,height:100px;}") = "<div style="width:200px,height:100px;"></div>"
	 * E [prop=value,..]           htmlz("input [type='text',name='user']") = "<input type="text" name="user" />"
	 * E+E                         htmlz("div+p") = "<div></div><p></p>"
	 * E>E                         htmlz("div>p") = "<div><p></p></div>"
	 * E*N                       htmlz("div>ul#list>li.item*3") =  "<div><ul id="list"><li class="item"></li><li class="item"></li><li class="item"></li></ul></div>"
	 */
	var htmlz = function(s) {
		return htmlz.method["main"].call({}, s);
	};
	hy.htmlz = htmlz;
	htmlz.regexp = {
		"node": /([a-zA-Z]\w*)/i,
		// 节点
		"id": /\#([a-zA-Z_]\w+)/i,
		// id
		"class": /\.([a-zA-Z_][\w\.]+)/i,
		// 类
		"style": /\{([\s\S]*?)\}/i,
		// 样式
		"attr": /\[(.*)\]/i,
		// 属性
		"split": /(\+|>)/,
		// 分隔符
		"num": /\*\s*(\d+)/,
		// 数量
		"singletag": /br|hr|img|input|param|meta|link/ // 单标签
	};
	htmlz.method = {
		main: function(s) {
			if (!s || !/^[a-zA-Z][\s\S]/.test(s)) return;
			var reg = htmlz.regexp;
			var nodes = s.split(reg["split"]);
			this[0] = [];
			var parent = this[0];
			for (var i in nodes) {
				var node = nodes[i];
				if (node == ">") {
					parent[parent.length - 1]["child"] = [];
					parent = parent[parent.length - 1]["child"];
				}
				if (i % 2 == 0) {
					htmlz.method["builder"].call(this, node, parent);
				}
			}
			return htmlz.method["recombine"].call(this, this[0]);
		},
		builder: function(node, parent) {
			var reg = htmlz.regexp,
				_node = reg["node"].exec(node),
				_id = reg["id"].exec(node),
				_class = reg["class"].exec(node),
				_style = reg["style"].exec(node),
				_attr = reg["attr"].exec(node),
				_num = reg["num"].exec(node);
			parent.push({
				"string": node,
				"node": (_node) ? _node[1] : null,
				"id": (_id) ? _id[1] : null,
				"class": (_class) ? _class[1].split(".").join(" ") : null,
				"style": (_style) ? _style[1] : null,
				"num": (_num) ? _num[1] : 1,
				"attribute": (_attr) ? _attr[1].split(",").join(" ") : null
			});
		},
		recombine: function(nodes) {
			var reg = htmlz.regexp,
				string = "";
			for (var i in nodes) {
				var node = nodes[i];
				var isSingleton = reg["singletag"].test(node["node"]);
				string += "<" + node["node"];
				if (node["id"]) {
					string += " id=\"" + node["id"] + "\"";
				}
				if (node["class"]) {
					string += " class=\"" + node["class"] + "\"";
				}
				if (node["style"]) {
					string += " style=\"" + node["style"] + "\"";
				}
				if (node["attribute"]) {
					string += " " + node["attribute"].replace(/'/g, "\"");
				}
				string += isSingleton ? "/>" : ">";
				if (node["child"]) {
					var child = htmlz.method["recombine"](node["child"]);
					string += child;
				}
				if (!isSingleton) string += "</" + node["node"] + ">";
				if (node["num"] > 1) {
					var temp = string;
					for (var i = node["num"]; i-- > 1;) {
						string += temp;
					}
				}
			}
			return string;
		}
	};

	/**
	 * 字符串格式化
	 *
	 * 参数说明
	 * format("hello! {0}, {1}.","tom","welcome") = "hello tom, welcome."
	 */
	hy.format = function() {
		var str = arguments[0],
			args = [];
		for (var i = 1,
				il = arguments.length; i < il; i++) {
			args.push(arguments[i]);
		}
		return str.replace(/\{(\d+)\}/g,
			function(m, i) {
				return args[i];
			});
	};

	/**
	 * 判断对象是否为数组
	 *
	 * 参数说明
	 * @param o javascrip对象
	 * isArray({}) = false;
	 * isArray([]) = true;
	 */
	hy.isArray = function(o) {
		return Object.prototype.toString.call(o) === "[object Array]";
	};

})(jQuery, window, document, undefined);